#!/usr/bin/python
# -*- coding: UTF-8 -*-
from selenium import webdriver;
from selenium.common.exceptions import TimeoutException
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import re;
from pyquery import PyQuery as pq;
from lxml import etree
import pymongo;
from  config import *
# browser=webdriver.PhantomJS(service_args=SERVICE_ARGS);
client = pymongo.MongoClient(MONGO_URL);
db = client["xiecheng"];
TABLE="hangzhou"
from bs4 import BeautifulSoup
import time
import requests
import pandas as pd
import requests
from utils.Yundama import YDMHttp

browser = webdriver.Chrome()
# browser.set_window_size(1400,900)
wait = WebDriverWait(browser, 10);

'''
广告弹窗处理
https://blog.csdn.net/zwq912318834/article/details/78612762
https://blog.csdn.net/xiaoyu_bug/article/details/73740547
'''


def search():
    url = "http://you.ctrip.com/sight/Hangzhou14.html"
    browser.get(url)
    pages = wait.until(
        EC.presence_of_element_located((By.CSS_SELECTOR, ".ttd_pager.cf .numpage"))
    )
    get_sight()
    return pages.text

def get_sight():
    pages = wait.until(
        EC.presence_of_element_located((By.CSS_SELECTOR, ".list_wide_mod2"))
    )
    html=browser.page_source
    # print(html)
    soup = BeautifulSoup(html, 'lxml')
    items=soup.select(".list_mod2")
    for item in items:
        price=item.select(".rdetailbox .price")
        i=item.select(".rdetailbox .price i")
        b=item.select(".rdetailbox .price .red")
        if i:
            i[0].extract()
        if b:
            b[0].extract()
        if price:
            price=price[0].string
        else:
            price=0
        sight={
            "title":item.select(".rdetailbox dt a")[0].attrs["title"],
            "url":"http://you.ctrip.com"+item.select(".rdetailbox dt a")[0].attrs["href"],
            "score":item.select(".r_comment .score strong")[0].string,
            "com_count":item.select(".r_comment .recomment")[0].string.strip(),
            "price":price
         }
        print(sight)
        saveToMongo(sight)

def mySelect(item,slect):
    pass


def saveToMongo(result):
    try:
        if db[TABLE].insert(result):
            print("save result success");
    except:
        print("save mongo error")

def next_page(page):
    print("currentpage", page)

    page_bu = browser.find_element_by_partial_link_text(u'下一页')
    browser.execute_script("arguments[0].scrollIntoView(false);", page_bu)

    next = wait.until(
        EC.element_to_be_clickable((By.CSS_SELECTOR,
                                    'body > div.ttd2_background > div > div.des_wide.f_right > div > div.list_wide_mod2 > div.ttd_pager.cf > div > a.nextpage')))
    clickHideButton(browser,next)
    wait.until(
        EC.text_to_be_present_in_element(
            (By.CSS_SELECTOR,
             'body > div.ttd2_background > div > div.des_wide.f_right > div > div.list_wide_mod2 > div.ttd_pager.cf > div > a.current'),
            str(page)
        )
    )
    get_sight()



#点击隐藏按钮
def clickHideButton(driver, element):
    driver.execute_script("arguments[0].click()", element);


def main():
    pages = int(search())
    for page in range(2, pages + 1):
        next_page(page)


if __name__ == '__main__':
    main()
